package com.sitanyun.merchant.guide.weiht;

import android.content.Context;

/**
 * Created by jzl
 * on 2022/3/30.
 */
public class ToolsUtil {
    private static ToolsUtil toolsUtil;
    private Context mContext;

    private ToolsUtil(Context context) {
        mContext = context.getApplicationContext();
    }

    public static ToolsUtil getInstance(Context context) {
        if (toolsUtil == null) {
            toolsUtil = new ToolsUtil(context);
        }
        return toolsUtil;
    }

    public static String timeConversion(int time) {
        int hour = 0;
        int minutes = 0;
        int sencond = 0;
        int temp = time % 3600;
        if (time > 3600) {
            hour = time / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    minutes = temp / 60;
                    if (temp % 60 != 0) {
                        sencond = temp % 60;
                    }
                } else {
                    sencond = temp;
                }
            }
        } else {
            minutes = time / 60;
            if (time % 60 != 0) {
                sencond = time % 60;
            }
        }
        return (hour<10?("0"+hour):hour) + ":" + (minutes<10?("0"+minutes):minutes) + ":" + (sencond<10?("0"+sencond):sencond);
    }

    public static String timeToString(long milliseconds) {
        final long s = 1000;
        final long m = 60 * s;
        final long h = 60 * m;
        final long d = 24 * h;

        long days = milliseconds / d;
        long hours = (milliseconds % d) / h;
        long minutes = (milliseconds % h) / m;
        long seconds = ((milliseconds % h) % m) / s;

        StringBuilder stringBuilder = new StringBuilder();
        if (days > 0) {
            stringBuilder.append(days + "天");
        } else if (hours > 0) {
            stringBuilder.append(hours + ":");
        } else if (minutes > 0) {
            stringBuilder.append(minutes + ":");
        } else if (seconds > 0) {
            stringBuilder.append(seconds + " ");
        }
        return stringBuilder.toString();
    }

    /**
     * 将秒数转换成天具体的天时分秒
     * 比如172800S转换成2天0时0分0秒
     * @param second
     * @return
     */
    public static String formatSecond(Object second){
        String  timeStr = "0秒";
        if(second!=null){
            Double s=(Double) second;
            String format;
            Object[] array;
            Integer days = (int)(s /(60 * 60 * 24));
            Integer hours =(int)(s/(60*60) - days * 24);
            Integer minutes = (int)(s/60 - hours*60 - days * 24 * 60);
            Integer seconds = (int)(s-minutes*60-hours*60*60 - days * 24 * 60 * 60);
            if(days>0){
                format="%1$,d天%2$,d时%3$,d分%4$,d秒";
                array=new Object[]{days,hours,minutes,seconds};
            } else if(hours>0){
                format="%1$,d时%2$,d分%3$,d秒";
                array=new Object[]{hours,minutes,seconds};
            }else if(minutes>0){
                format="%1$,d分%2$,d秒";
                array=new Object[]{minutes,seconds};
            }else{
                format="%1$,d秒";
                array=new Object[]{seconds};
            }
            timeStr = String.format(format, array);
        }
        return timeStr ;
    }
}
